package com.mobileares.midp.mui.client.form;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.widgets.client.textbox2.*;
import com.wwfish.gwt.model.client.IFishModel;
import com.wwfish.gwt.model.client.impl.FishModelManager;
import com.wwfish.gwt.model.client.impl.FormModel;
import com.wwfish.gwt.model.client.mui.BMWidget;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-11-24
 * Time: 15:06:25
 * To change this template use File | Settings | File Templates.
 */
public class UFishFlowForm<T> extends BMWidget<FlexTable, T, FormModel<T>> {
    private LabelContainer form;
    private FlexTable container = new FlexTable();
    private int col;

    public UFishFlowForm(int col) {
        this.col = col;
        form = new LabelContainer(true);
        initWidget(container);
    }

    public void ddOutModel() {
        form.ddOutModel(getFishModel().getModel());
    }

    public boolean ddIntModel() {
        if (form.fire()) {
            form.ddIntModel(getFishModel().getModel());
            return true;
        } else
            return false;
    }

    private void addElement(LabelProvider element) {
        form.registerElement(element);
        addWidget(element.getWidget());
    }

    public void addElement(final BaseLabelText widget, final String field) {
        addElement(widget, field, null);
    }

    public void addElement(final BaseLabelText widget, final String field, ILabelValidator[] validators) {
        BaseLabelTextWrapper wrapper;
        wrapper = new BaseLabelTextWrapper(fishWidget, widget, field);
        initElement(wrapper, field, validators);
    }

    public void addElement(BaseLabelList widget, String field, IFishModel codesModel) {
        addElement(widget, field, codesModel, null);
    }

    public void addElement(BaseLabelList widget, String field, IFishModel codesModel, ILabelValidator[] validators) {
        BaseLabelListWrapper wrapper;
        wrapper = new BaseLabelListWrapper(fishWidget, widget, field);
        wrapper.setFishModel(codesModel);
        initElement(wrapper, field, validators);
    }

    public void addElement(BaseLabelList widget, String field, List codes) {
        addElement(widget, field, codes, null);
    }

    public void addElement(BaseLabelList widget, String field, List codes, ILabelValidator[] validators) {
        BaseLabelListWrapper wrapper;
        wrapper = new BaseLabelListWrapper(fishWidget, widget, field);
        IFishModel model = FishModelManager.createTempModel();
        model.load(codes);
        wrapper.setFishModel(model);
        initElement(wrapper, field, validators);
    }

    private void initElement(BaseLabelTextWrapper widget, final String field, ILabelValidator[] validators) {
        LabelProvider el = new LabelProvider(widget);
        el.setId(field);
        el.setWidget((Widget) widget.getBMUI());
        if (validators != null && validators.length > 0) {
            for (ILabelValidator v : validators)
                el.addValidators(v);
        }
        addElement(el);
    }

    private void addWidget(Widget widget, HasHorizontalAlignment.HorizontalAlignmentConstant hAlign, HasVerticalAlignment.VerticalAlignmentConstant vAlign) {
        int row;
        int col;

        if (container.getRowCount() > 0 && container.getCellCount(container.getRowCount() - 1) >= this.col) {
            row = container.getRowCount();
            col = 0;
        } else {
            if (container.getRowCount() == 0) {
                row = 0;
                col = 0;
            } else {
                row = container.getRowCount() - 1;
                col = container.getCellCount(container.getRowCount() - 1);
            }
        }

        container.setWidget(row, col, widget);
        container.getCellFormatter().setAlignment(row, col, hAlign, vAlign);
    }

    public void addWidget(Widget widget) {
        addWidget(widget, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_MIDDLE);
    }

    public void addWidget(Widget widget, HasHorizontalAlignment.HorizontalAlignmentConstant hAlign) {
        addWidget(widget, hAlign, HasVerticalAlignment.ALIGN_TOP);
    }

}
